import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-07-09
 * 10:53
 */
public class Solution {

    public static class ListNode {//单链表
      int val;
      ListNode next;
      ListNode() {}
     ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

    public static ListNode middleNode(ListNode head) {//找链表中间节点  原理fast=2slow；
        ListNode slow=head;//慢指针
        ListNode fast=head;//快指针
        while(fast!=null&&fast.next!=null ){//条件不能互换，因为如果head==null，则会空指针异常
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;//即中间节点

    }

    public static void main(String[] args) {
        ListNode listNode=new ListNode(1);
        ListNode listNode1=new ListNode(2);
        ListNode listNode2=new ListNode(3);
        ListNode head=listNode;
        listNode.next=listNode1;
        listNode1.next=listNode2;

        System.out.println(middleNode(head).val);
    }


}
